package com.example.helloworld.view;

import com.example.helloworld.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Join;
import android.graphics.Paint.Style;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

public class CustomViewDrawable extends View {

	final private ShapeDrawable upperDrawable;
	final private ShapeDrawable lowerDrawable;
	
	@SuppressLint("NewApi")
	public CustomViewDrawable(Context context) {
		super(context);
		
		WindowManager manager =	(WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
		Display display =		manager.getDefaultDisplay();
		Point sizePoint =		new Point();
		display.getSize(sizePoint);
		int width =				sizePoint.x;
		int height =			sizePoint.y;
		
		Resources resources =	getResources();
		int black =				resources.getColor(R.color.color01);
		int gray =				resources.getColor(R.color.color02);
		int darkGray =			resources.getColor(R.color.color03);
		
		upperDrawable =			new ShapeDrawable();
		
		{
			RectShape rect =		new RectShape();
			rect.resize(width, height * 2 / 3);
			upperDrawable.setShape(rect);
			upperDrawable.setBounds(0, 0, width, height * 2 / 3);
		
			LinearGradient gradient = 
								new LinearGradient(0, 0, 0, height * 2 / 3, gray, black, TileMode.CLAMP);
			
			Paint paint =		upperDrawable.getPaint();
			paint.setShader(gradient);
		}
		
		lowerDrawable =			new ShapeDrawable();
		
		{
			RectShape rect =		new RectShape();
			rect.resize(width, height * 1 / 3);
			lowerDrawable.setShape(rect);
			lowerDrawable.setBounds(0, height * 2 / 3, width, height);
			
			LinearGradient gradient =
									new LinearGradient(0, 0, 0, height * 1 / 3, black, darkGray, TileMode.CLAMP);
			
			Paint paint =		lowerDrawable.getPaint();
			paint.setShader(gradient);
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		upperDrawable.draw(canvas);
		lowerDrawable.draw(canvas);
		
		Paint paint =	new Paint();
		paint.setAntiAlias(true);
		paint.setColor(Color.YELLOW);
		paint.setStyle(Style.STROKE);
		paint.setStrokeWidth(16.0F);
		paint.setStrokeCap(Cap.BUTT);
		paint.setStrokeJoin(Join.MITER);
		
		
		Path path =		new Path();
		path.moveTo(20, 20);
		path.lineTo(120, 20);
		path.lineTo(160, 90);
		path.lineTo(180, 80);
		path.lineTo(200, 120);
		
		canvas.drawPath(path, paint);
		
		paint.setColor(Color.WHITE);
		paint.setStrokeCap(Cap.ROUND);
		paint.setStrokeJoin(Join.ROUND);
		
		path.offset(30, 120);
		canvas.drawPath(path, paint);
		
		paint.setColor(Color.CYAN);
		paint.setStrokeCap(Cap.SQUARE);
		paint.setStrokeJoin(Join.BEVEL);
		
		path.offset(30, 120);
		canvas.drawPath(path, paint);
	}
	
}
